using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace XNATetris
{
    public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont font;
        Texture2D menuBackground;
        Rectangle playRec;
        Rectangle exitRec;
        Rectangle resumeRec;

        public Menu(Game game)
            : base(game)
        {
            
        }

        public override void Initialize()
        {
            Game.IsMouseVisible = true;

            inputService = null;
            input = null;

            base.Initialize();
        }

        IInputService inputService;

        bool up = false;
        bool down = false;
        bool enter = false;

        Input input;

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Game.Content.Load<SpriteFont>("fontMenu");

            menuBackground = Game.Content.Load<Texture2D>("TetrisMenu");

            playRec = new Rectangle(100, 300, (int)font.MeasureString("Play").X, (int)font.MeasureString("Play").Y);
            exitRec = new Rectangle(100, 350, (int)font.MeasureString("Exit").X, (int)font.MeasureString("Exit").Y);
            resumeRec = new Rectangle(100, 400, (int)font.MeasureString("Resume Game").X, (int)font.MeasureString("Resume Game").Y);

            input = new Input(Game);
            Game.Components.Add(input);

            inputService = Game.Services.GetService(typeof(IInputService)) as IInputService;
            inputService.UpOn += () => up = true;
            inputService.UpOff += () => up = false;

            inputService.DownOn += () => down = true;
            inputService.DownOff += () => down = false;

            inputService.EnterOn += () => enter = true;
            inputService.EnterOff += () => enter = false;

            base.LoadContent();
        }

        uint select = 0;
        uint selectRange = 2;
        public override void Update(GameTime gameTime)
        {
#if WINDOWS
            if (playRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0))
                &&
                Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                while (Game.Components.Count > 0)
                {
                    Game.Components.RemoveAt(0);
                }
                Game.Services.RemoveService(typeof(IInputService));

                Game.Components.Add(new OnePlayerMode(Game));
            }

            if (exitRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0))
                &&
                Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                Game.Exit();
            }

            if (Game.Components.Count > 2)
            {
                if (resumeRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0))
                    &&
                    Mouse.GetState().LeftButton == ButtonState.Pressed)
                {
                    foreach (GameComponent item in Game.Components)
                    {
                        item.Enabled = true;
                    }

                    Game.Services.RemoveService(typeof(IInputService));
                    Game.Components.Remove(input);
                    Game.Components.Remove(this);
                }
            }
#endif
            if (Game.Components.Count > 2)
            {
                selectRange = 2;
            }
            else
                selectRange = 1;

            if (up && (select > 0))
                select -= 1;
            if (down && (select < selectRange))
                select += 1;
            if (enter)
            {
                switch (select)
                {
                    case 0:
                        while (Game.Components.Count > 0)
                        {
                            Game.Components.RemoveAt(0);
                        }
                        Game.Services.RemoveService(typeof(IInputService));

                        Game.Components.Add(new OnePlayerMode(Game));
                        break;
                    case 1:
                        Game.Exit();
                        break;
                    case 2:
                        foreach (GameComponent item in Game.Components)
                        {
                            item.Enabled = true;
                        }

                        Game.Services.RemoveService(typeof(IInputService));
                        Game.Components.Remove(input);
                        Game.Components.Remove(this);
                        break;
                    default:
                        break;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            spriteBatch.Draw(menuBackground, Vector2.Zero, Color.White);

#if WINDOWS
            if (playRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0)))
            {
                spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Blue);
                spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Green);
                if (Game.Components.Count > 2)
                    spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Green);
            }
            else if (exitRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0)))
            {
                spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Blue);
                spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Green);
                if (Game.Components.Count > 2)
                    spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Green);
            }
            else if (resumeRec.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 0, 0)))
            {
                if (Game.Components.Count > 2)
                {
                    spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Blue);
                    spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Green);
                    spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Green);
                }
            }
            else
            {
#endif
                if (select == 0)
                {
                    spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Blue);
                    spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Green);
                    if (Game.Components.Count > 2)
                        spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Green);
                }
                else if (select == 1)
                {
                    spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Blue);
                    spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Green);
                    if (Game.Components.Count > 2)
                        spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Green);
                }
                else if (select == 2)
                {
                    spriteBatch.DrawString(font, "Resume Game", new Vector2(100.0f, 400.0f), Color.Blue);
                    spriteBatch.DrawString(font, "Exit", new Vector2(100.0f, 350.0f), Color.Green);
                    spriteBatch.DrawString(font, "Play", new Vector2(100.0f, 300.0f), Color.Green);
                }
#if WINDOWS
            }
#endif

            spriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
